<?php

/**
 * Content form base class.
 *
 * @method Content getObject() Returns the current form's model object
 *
 * @package    ##PROJECT_NAME##
 * @subpackage form
 * @author     ##AUTHOR_NAME##
 */
abstract class BaseContentForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                      => new sfWidgetFormInputHidden(),
      'is_active'               => new sfWidgetFormInputCheckbox(),
      'hits'                    => new sfWidgetFormInputText(),
      'user_id'                 => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => false)),
      'type'                    => new sfWidgetFormInputText(),
      'category_id'             => new sfWidgetFormPropelChoice(array('model' => 'Category', 'add_empty' => true)),
      'created_at'              => new sfWidgetFormDateTime(),
      'updated_at'              => new sfWidgetFormDateTime(),
      'created_by'              => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => true)),
      'link_content_asset_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'sfAsset')),
    ));

    $this->setValidators(array(
      'id'                      => new sfValidatorPropelChoice(array('model' => 'Content', 'column' => 'id', 'required' => false)),
      'is_active'               => new sfValidatorBoolean(),
      'hits'                    => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647)),
      'user_id'                 => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id')),
      'type'                    => new sfValidatorInteger(array('min' => -128, 'max' => 127, 'required' => false)),
      'category_id'             => new sfValidatorPropelChoice(array('model' => 'Category', 'column' => 'id', 'required' => false)),
      'created_at'              => new sfValidatorDateTime(array('required' => false)),
      'updated_at'              => new sfValidatorDateTime(array('required' => false)),
      'created_by'              => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id', 'required' => false)),
      'link_content_asset_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'sfAsset', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('content[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Content';
  }

  public function getI18nModelName()
  {
    return 'ContentI18n';
  }

  public function getI18nFormClass()
  {
    return 'ContentI18nForm';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['link_content_asset_list']))
    {
      $values = array();
      foreach ($this->object->getLinkContentAssets() as $obj)
      {
        $values[] = $obj->getAssetId();
      }

      $this->setDefault('link_content_asset_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveLinkContentAssetList($con);
  }

  public function saveLinkContentAssetList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['link_content_asset_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(LinkContentAssetPeer::CONTENT_ID, $this->object->getPrimaryKey());
    LinkContentAssetPeer::doDelete($c, $con);

    $values = $this->getValue('link_content_asset_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new LinkContentAsset();
        $obj->setContentId($this->object->getPrimaryKey());
        $obj->setAssetId($value);
        $obj->save();
      }
    }
  }

}
